﻿using Daemon.Common.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Daemon.Common.Generator
{
	public class UploadImgComponentGenerator : ComponentGenerator
	{
		public override string WidgetTypeName => "UploadImg";

		public override ComponentResponse DoGenerate(Component component)
		{
			var response = new ComponentResponse();
			StringBuilder strResult = new StringBuilder();
			strResult.Append($"<Form.Item label='{component.Title}' name='{component.Name}' ");
			if (component.Required)
			{
				strResult.Append("rules={[{ required: true, message: '" + component.Message + "' }]}");
			}
			strResult.Append(" >");
			var bindHtml = string.Empty;
			if (!string.IsNullOrEmpty(component.Bind))
			{
				bindHtml = $"value={{{component.Bind}}}";
			}
			strResult.Append($" <input type='hidden' name='{component.Name}' {bindHtml} />");
			strResult.Append($"<{WidgetTypeName} callBack={{set{component.Bind}}} />");
			strResult.Append($"</Form.Item>");
			response.ComponentHtml = strResult.ToString();
			response.VariableList = GenerateVariable(component);
			return response;
		}

		public override List<string> GenerateVariable(Component component)
		{
			var variableResult = new List<string>();
			if (!string.IsNullOrEmpty(component.Bind))
			{
				variableResult.Add($" const [{component.Bind.CapitalizeLower()},set{component.Bind}] = useState<any>(); ");

			}
			return variableResult;
		}
	}
}
